package org.dromara.commission.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.commission.domain.LzshHandworkCommission;
import org.dromara.commission.domain.bo.LzshNetWorkLatitudeBo;
import org.dromara.commission.domain.vo.LzshNetWorkImportVo;
import org.dromara.commission.domain.vo.LzshNetWorkLatitudeVo;
import org.dromara.commission.service.ILzshNetWorkLatitudeService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.satoken.utils.LoginHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : sxl
 * @date : 2024/7/27 11:36
 * @Version: 1.0
 */
@Slf4j
public class LzshNetWorkImportListener extends AnalysisEventListener<LzshNetWorkImportVo> implements ExcelListener<LzshNetWorkImportVo> {
    private final ILzshNetWorkLatitudeService lzshNetworkLatitudeService;

    private final Boolean isUpdateSupport;

    private final Long operUserId;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    private final List<LzshHandworkCommission> list = new ArrayList<>();

    public LzshNetWorkImportListener(Boolean isUpdateSupport) {
        this.lzshNetworkLatitudeService = SpringUtils.getBean(ILzshNetWorkLatitudeService.class);;
        this.isUpdateSupport = isUpdateSupport;
        this.operUserId = LoginHelper.getUserId();
    }



    @Override
    public ExcelResult<LzshNetWorkImportVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<LzshNetWorkImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }

    @Override
    public void invoke(LzshNetWorkImportVo lzshNetWorkImportVo, AnalysisContext analysisContext) {
        LzshNetWorkLatitudeVo lzshNetWork = this.lzshNetworkLatitudeService.queryByBusinessCode(lzshNetWorkImportVo.getBusinessCode(), lzshNetWorkImportVo.getAgentName(), lzshNetWorkImportVo.getAccountPeriod());
        // 判断这条数据是否该商家下个月的网点维度
        if (!ObjectUtil.isNull(lzshNetWork)) {
            //是，就把这个月的全部删除，添加下个月的
            Boolean res = lzshNetworkLatitudeService.deleteByBcAndAn(lzshNetWorkImportVo.getBusinessCode(), lzshNetWorkImportVo.getAgentName());
            LzshNetWorkLatitudeBo networkLatitudeBo = BeanUtil.toBean(lzshNetWorkImportVo, LzshNetWorkLatitudeBo.class);
            ValidatorUtils.validate(networkLatitudeBo);
            networkLatitudeBo.setCreateBy(operUserId);
            lzshNetworkLatitudeService.insertByBo(networkLatitudeBo);
            successNum++;
            successMsg.append("<br/>").append(successNum).append("、网点维度 ").append(networkLatitudeBo.getAgentName()).append(" 更新成功");
        } else {
            //不是(其他商家的数据或者这个商家这个月的数据)，就直接插入
            LzshNetWorkLatitudeBo networkLatitudeBo = BeanUtil.toBean(lzshNetWorkImportVo, LzshNetWorkLatitudeBo.class);
            ValidatorUtils.validate(networkLatitudeBo);
            networkLatitudeBo.setCreateBy(operUserId);
            lzshNetworkLatitudeService.insertByBo(networkLatitudeBo);
            successNum++;
            successMsg.append("<br/>").append(successNum).append("、网点维度 ").append(networkLatitudeBo.getAgentName()).append(" 导入成功");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
